<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/axios.min.js"></script>
    <script src="/js/query.js"></script>

    <script>
        var commodityId;
        window.onload = function (){
            //得到从前一页面提交的商品ID
            commodityId = getQuery("id");
            findcommodity();
        }

        function findcommodity(){
            axios.get("/commodity/findById",{
                params:{
                    id:commodityId
                }
            }).then(resp =>{
                var info = resp.data;
                $("commodityImg").src = "/img/commodity/"+info.imgPath;
                $("nameSpan").innerHTML = info.name;
                $("priceSpan").innerHTML = info.price;
                $('commentNumSpan').innerHTML = info.commentNum;

                this.fullTable(info.commentList);
            })
        }

        /**
         * 填充评论表格
         * @param commentList
         */
        function fullTable(commentList){
            var str = "";
            commentList.forEach(n =>{
                str += `<tr><td>${n.user.nickName}</td><td>${n.content}</td></tr>`;
            });
            $("data").innerHTML = str;
        }

        /**
         * 添加评论
         */
        function addComment(){
            axios.get("/comment/add",{
                params:{
                    commodityId,
                    content:$("content").value
                }
            }).then(resp =>{
                let info =resp.data;
                if (info=="noland"){
                    location.href="/login.html?errorInfo=noland";
                }
                fullTable(resp.data);
            });
        }
    </script>
</head>
<body>
<div>
    <img src="" id="commodityImg" width="200" height="200"><br>
    商品名：<span id="nameSpan"></span><br>
    单价：<span id="priceSpan"></span><br>
    评论：<span id="commentNumSpan"></span><br>

    <table width="50%" border="1">
        <tbody id="data"></tbody>
    </table>

    请输入评价:<input type="text" id="content">
    <input type="button" value="评论" onclick="addComment()">
</div>
</body>
</html>